using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;

namespace Traceability.API.Services
{
    /// <summary>
    /// 验证码服务
    /// </summary>
    public class CaptchaService
    {
        private readonly Random _random = new Random();

        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="code">验证码文本</param>
        /// <param name="width">图片宽度</param>
        /// <param name="height">图片高度</param>
        /// <returns>验证码图片的字节数组</returns>
        public byte[] GenerateCaptchaImage(string code, int width = 120, int height = 40)
        {
            using (var bitmap = new Bitmap(width, height))
            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                graphics.Clear(Color.White);
                DrawInterferenceLines(graphics, width, height);
                DrawInterferencePoints(graphics, width, height);
                DrawCaptchaTextOptimized(graphics, code, width, height);
                using (var memoryStream = new MemoryStream())
                {
                    bitmap.Save(memoryStream, ImageFormat.Png);
                    return memoryStream.ToArray();
                }
            }
        }

        /// <summary>
        /// 生成随机验证码
        /// </summary>
        public string GenerateRandomCode(int length = 4)
        {
            const string chars = "ABCDEFGHJKMNPQRSTUVWXYZ23456789";
            return new string(Enumerable.Repeat(chars, length)
                .Select(s => s[_random.Next(s.Length)]).ToArray());
        }

        private void DrawInterferenceLines(Graphics graphics, int width, int height)
        {
            var colors = new[] { Color.LightGray, Color.LightBlue, Color.LightGreen, Color.LightPink };
            for (int i = 0; i < 3; i++)
            {
                var pen = new Pen(colors[_random.Next(colors.Length)], 1);
                var x1 = _random.Next(width);
                var y1 = _random.Next(height);
                var x2 = _random.Next(width);
                var y2 = _random.Next(height);
                graphics.DrawLine(pen, x1, y1, x2, y2);
            }
        }

        private void DrawInterferencePoints(Graphics graphics, int width, int height)
        {
            var colors = new[] { Color.LightGray, Color.LightBlue, Color.LightGreen };
            for (int i = 0; i < 50; i++)
            {
                var brush = new SolidBrush(colors[_random.Next(colors.Length)]);
                var x = _random.Next(width);
                var y = _random.Next(height);
                graphics.FillEllipse(brush, x, y, 1, 1);
            }
        }

        private void DrawCaptchaTextOptimized(Graphics graphics, string code, int width, int height)
        {
            Font font;
            try { font = new Font("Arial", 22, FontStyle.Bold); }
            catch { font = SystemFonts.DefaultFont; }
            var colors = new[] { Color.Black, Color.DarkBlue, Color.DarkGreen, Color.DarkRed };
            var sizeF = graphics.MeasureString(code, font);
            float startX = (width - sizeF.Width) / 2;
            float startY = (height - sizeF.Height) / 2;
            for (int i = 0; i < code.Length; i++)
            {
                var brush = new SolidBrush(colors[_random.Next(colors.Length)]);
                float charWidth = sizeF.Width / code.Length;
                float x = startX + i * charWidth + _random.Next(-2, 2);
                float y = startY + _random.Next(-2, 2);
                graphics.DrawString(code[i].ToString(), font, brush, x, y);
            }
        }
    }
} 